<?php

namespace App\Http\Controllers\Guard\School;

use App\Http\Controllers\Common\GuardController;
use App\Models\Advert\Advert;
use App\Models\Article\ArticleInfo;
use App\Models\School\School;
use App\Service\SchoolService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class SchoolController extends GuardController {

    private $service;

    public function __construct() {
        $this->service = new SchoolService(School::class);
    }

    public function change(Request $request) {
        $id = $request->input('id',0);
        if($request->has('status')){
            $this->service->update($id, 'status', $request->input('status'));
        }elseif ($request->has('is_top')){
            $this->service->update($id, 'is_top', $request->input('is_top'));
        }else{

        }
        return responseSuccess();

    }

    public function lst(Request $request) {
        $conditions = $request->input();
        $records = $this->service->lst($conditions)
            ->paginate($conditions['pageSize']??20,['*'],'page',$conditions['page']??1);
        foreach ($records as $record) {
            $record->pic_url = asset($record->pic_url);
        }

        return responseSuccess($records->items(),$records->total());
    }

    public function add(Request $request) {

        $this->validate($request, [
            'name'=>'required|min:1|max:20',
            //'summary'=>'required|min:1|max:100',
            'pic_url'=>'required',
            'content'=>'required'
        ],[],[
            'name'=>'院校名称',
            'pic_url'=>'图片',
            'content'=>'院校介绍',
        ]);

        $info = $request->input();

        $token = $request->header('token');
        if(empty($token)) return responseFail('获取token失败！');
        $id = checkJWT($token)['id']??0;
        if($id<=0){
            return responseFail('获取当前用户失败！');
        }
        $info['guard_user_id'] = $id;

        $this->service->add($info);
        return responseSuccess();
    }

    public function detail(Request $request) {
        $id = $request->input('id',0);
        $record = $this->service->detail($id);
        return responseSuccess([$record->toArray()],1);
    }

    public function del(Request $request) {
        $ids = $request->input('ids',[]);
        $this->service->del($ids);
        return responseSuccess();
    }

    public function getCountry(Request $request) {

        $records = DB::table('country')->get(['id as value','name as label']);
        return responseSuccess($records->toArray(),$records->count());
    }

    public function getTemplateContent(Request $request){
        $id = $request->input('id');
        if($request->isMethod('GET')){
            $icons = [
                ['is_show'=>true,'path'=>"/common/image/school/1.png", 'url'=>asset("/common/image/school/1.png"),'name'=>'线上报名'],
                ['is_show'=>true,'path'=>"/common/image/school/2.png", 'url'=>asset("/common/image/school/2.png"),'name'=>'资格审核'],
                ['is_show'=>true,'path'=>"/common/image/school/3.png", 'url'=>asset("/common/image/school/3.png"),'name'=>'现场复试'],
                ['is_show'=>true,'path'=>"/common/image/school/4.png", 'url'=>asset("/common/image/school/4.png"),'name'=>'奖学金资格发放'],
                ['is_show'=>true,'path'=>"/common/image/school/5.png", 'url'=>asset("/common/image/school/5.png"),'name'=>'发送录取通知书'],
                ['is_show'=>true,'path'=>"/common/image/school/6.png", 'url'=>asset("/common/image/school/6.png"),'name'=>'签证培训'],
                ['is_show'=>true,'path'=>"/common/image/school/7.png", 'url'=>asset("/common/image/school/7.png"),'name'=>'赴美'],
            ];
            //dd($id);
            $school = $this->service->detail($id);
            $article = [];
            if(!empty($school)){
                $article = $school->getArticleInfo();
            }
            return responseSuccess(compact('icons','school','article'));
        }else{
            $template_content = $request->input('template_content');
            $is_finished_template = $request->input('is_finished_template');
            Log::info('json',$template_content);
            //dd($template_content);
            $template_content = json_encode($template_content,JSON_UNESCAPED_UNICODE);
            School::where(compact('id'))->update(compact('template_content','is_finished_template'));
            //dd(1);
            return responseSuccess();
        }


    }



}
